/*********** tuannh ****************
 function : tools line, polygon extension
***********************************/
var gPointCount = 0;
var pointArr = [];
var pointLast_Path = null;
var bUndo_First = true;
var _begin_Draw = false;
var feature_Perpendicular_Parallel_Direction = null;
var feature_Graphics = null;
var Select_Extension = -1; // Portlet_Type_Action.None;
var _divMenuAction_Current = null;
var dGoc_FeatureSelected_Parallel_Perpendicular = 0;
function event_AddFeature(obj) {
	var objAdd_Extension = obj;
	if (!objAdd_Extension.checked) {
		switch (objAdd_Extension.value) {
		case 'angle':
			Select_Extension = Portlet_Type_Action.Add_Line_Extension_Angle;
			break;
		case 'parallel':
			Select_Extension = Portlet_Type_Action.Add_Line_Extension_Parallel;
			break;
		case 'distance':
			Select_Extension = Portlet_Type_Action.Add_Line_Extension_Distance;
			break;
		case 'vertext':
			Select_Extension = Portlet_Type_Action.Add_Vertex;
			break;
		case 'copy':
			Select_Extension = Portlet_Type_Action.Copy;
			break;
		default:
			Select_Extension = -1;
			break;
		}
	}
}

function btn_Map_AddFeature_Click() {
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	var layer_Activate = map.layers[indexLayer];
	if (feature_Current_Action != null)
		layer_Activate.drawFeature(feature_Current_Action, "default");
	if (_divMenuAction_Current != null)
		_divMenuAction_Current.style.display = "none";
	indexLayer = indexLayer - 1 - map_WMS.length;
	switch (Select_Extension) {
	case Portlet_Type_Action.Add_Line_Extension_Angle:
		_divMenuAction_Current = _div_Map_DirectionFeature;
		Draw_Obj[indexLayer].handler.mousemove = mousemoveHandler_Path;
		Draw_Obj[indexLayer].activate();
		break;
	case Portlet_Type_Action.Add_Line_Extension_Parallel:
		_divMenuAction_Current = _div_Map_ParallelFeature;
		Draw_Obj[indexLayer].handler.mousemove = mousemoveHandler_Path;
		Draw_Obj[indexLayer].activate();
		break;
	case Portlet_Type_Action.Add_Line_Extension_Distance:
		_divMenuAction_Current = null;
		break;
	case Portlet_Type_Action.Add_Vertex:
		_divMenuAction_Current = null;
		Modify_Obj[indexLayer].activate();
		Modify_Obj[indexLayer].mode = OpenLayers.Control.ModifyFeature.RESHAPE;
		Modify_Obj[indexLayer].createVertices = true;
		break;
	case Portlet_Type_Action.Copy:
		_divMenuAction_Current = null;
		break;
	default:
		_divMenuAction_Current = null;
		break;
	}
	if (_divMenuAction_Current != null) {
		_divMenuAction_Current.style.display = "block";
		var top = Panel_Current.divControl.offsetTop
				+ Panel_Current.divControl.offsetHeight;
		var left = map_Left + map_Width - _divMenuAction_Current.offsetWidth;
		_divMenuAction_Current.style.top = (top + 5) + "px";
		_divMenuAction_Current.style.left = (left - 5) + "px";
	}
	Panel_Current.current_ImgAction.divContainer.divChild = _divMenuAction_Current;
	// alert(lll.abc);
}

function ExtensionFeature_Close() {
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	var layer_Activate = map.layers[indexLayer];
	if (feature_Current_Action != null)
		layer_Activate.drawFeature(feature_Current_Action, "default");
	if (_divMenuAction_Current != null)
		_divMenuAction_Current.style.display = "none";
	_begin_Draw = false;
	Select_Extension = -1;
	return false;
}

function createLine(layer, point1, point2) {
	var pointList = [];
	pointList.push(point1);
	pointList.push(point2);
	var line = new OpenLayers.Geometry.LineString(pointList);
	var style = {
		strokeColor : colorSelected_Line_Delete,
		strokeOpacity : 0.8,
		strokeWidth : 3
	};

	var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
	// lineFeature.state = OpenLayers.State.INSERT;
	layer.addFeatures([ lineFeature ]);
	return lineFeature;
}

function createPoint(layer, point) {
	var pPoint = new OpenLayers.Geometry.Point();
	pPoint.x = point.x;
	pPoint.y = point.y;
	var style = {
		strokeColor : '#FF0000',
		strokeOpacity : "0.7",
		strokeWidth : 2,
		fillColor : '#FF0000',
		pointRadius : 5,
		cursor : "pointer"
	};
	var pointFeature = new OpenLayers.Feature.Vector(pPoint, null, style);
	layer.addFeatures([ pointFeature ]);
	return pPoint;
}

// xac dinh line de song song hoac vuong goc tu 1 polyline ma nguoi dung
// right-click
function findLine_Parallel_Perpendicular(bounds, polyline) {
	var i = 0;
	var lineFeature = null;
	var point1, point2;
	var boundsIntersect;
	var objComponents = polyline.geometry.components[0].components;
	for (i = 0; i < objComponents.length - 1; i++) {
		point1 = objComponents[i];
		point2 = objComponents[i + 1];
		var xMin = point1.x < point2.x ? point1.x : point2.x;
		var xMax = point1.x < point2.x ? point2.x : point1.x;
		var yMin = point1.y < point2.y ? point1.y : point2.y;
		var yMax = point1.y < point2.y ? point2.y : point1.y;
		boundsIntersect = new OpenLayers.Bounds(xMin, yMin, xMax, yMax);
		if (bounds.intersectsBounds(boundsIntersect)) {
			vectorGraphics.removeAllFeatures();
			lineFeature = createLine(vectorGraphics, point1, point2);
			feature_Graphics = lineFeature;
			var vector_Line = create_Vector(point1, point2);
			// vector_Line = new Line_Vector_Decac(-0.5773672, 1, 2);
			dGoc_FeatureSelected_Parallel_Perpendicular = Goc_DuongThang(vector_Line);
			/*
			 * if (Action_Current == Portlet_Type_Action.Perpendicular_Line) { var
			 * _txtGoc_Perpendicular = _getElementById('txtGoc_Perpendicular');
			 * var obj = _txtGoc_Perpendicular.value; if (obj == "")
			 * _txtGoc_Perpendicular.value = obj = 90; else obj = Number(obj);
			 * dGoc_FeatureSelected_Parallel_Perpendicular =
			 * dGoc_FeatureSelected_Parallel_Perpendicular + obj; }
			 */
			break;
		}
	}
}

function timeout_Clear_Line() {
	vectorGraphics.removeAllFeatures();
	feature_Graphics = null;
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	if (feature_Perpendicular_Parallel_Direction != null)
		map.layers[indexLayer].drawFeature(
				feature_Perpendicular_Parallel_Direction, "default");
}

/*
 * event click point on map when draw
 */
function pointHandler_Path(aPoint) {
	timeout_Clear_Line();
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	switch (Select_Extension) {
	case Portlet_Type_Action.Add_Line_Extension_Angle:
		var objComponentLength = this.handler.getGeometry().components[0].components;
		var _chkRelative = _getElementById('chk_Map_Relative');
		if (objComponentLength.length > 2) {
			_chkRelative.disabled = false;
			var bSame_Coordinate = true;
			var indexComponents = objComponentLength.length - 2;
			while (bSame_Coordinate) {
				if ((objComponentLength[indexComponents + 1].x != objComponentLength[indexComponents].x)
						|| (objComponentLength[indexComponents + 1].y != objComponentLength[indexComponents].y)) {
					bSame_Coordinate = false;
				} else
					indexComponents -= 1;
			}
			point1 = objComponentLength[indexComponents];
			point2 = objComponentLength[indexComponents + 1];
			var vector_Line = create_Vector(point1, point2);
			dGoc_FeatureSelected_Parallel_Perpendicular = Goc_DuongThang(vector_Line);
			if (dGoc_FeatureSelected_Parallel_Perpendicular > 180)
				dGoc_FeatureSelected_Parallel_Perpendicular -= 180;
			if ((point1.x > point2.x) && (point1.y > point2.y)) {
				dGoc_FeatureSelected_Parallel_Perpendicular += 180;
			} else if ((point1.x > point2.x) && (point1.y < point2.y)) {
			}
			if ((point1.x < point2.x) && (point1.y > point2.y)) {
				dGoc_FeatureSelected_Parallel_Perpendicular -= 180;
			}
			if ((point1.x < point2.x) && (point1.y < point2.y)) {
			}
		} else
			_chkRelative.disabled = true;
		break;
	case Portlet_Type_Action.Add_Line_Extension_Parallel:
		if (OpenLayers.Event.isRightClick(event)) {
			var i = 0;
			var tolerance = 50;
			var bounds = new OpenLayers.Bounds(aPoint.x - tolerance, aPoint.y
					- tolerance, aPoint.x + tolerance, aPoint.y + tolerance);
			var bFindFeature = false;
			var _txtFeatureSelected = null;
			_txtFeatureSelected = _getElementById('txt_Map_FeatureSelected_Parallel');
			for (i = 0; i < map.layers[indexLayer].features.length; i++) {
				feature_Perpendicular_Parallel_Direction = map.layers[indexLayer].features[i];
				var b = feature_Perpendicular_Parallel_Direction.geometry.bounds;
				if (bounds.intersectsBounds(b)) {
					map.layers[indexLayer].drawFeature(
							feature_Perpendicular_Parallel_Direction, "select");
					if (_txtFeatureSelected != null)
						_txtFeatureSelected.value = feature_Perpendicular_Parallel_Direction.fid;
					_begin_Draw = true;
					bUndo_First = true;
					bFindFeature = true;
					findLine_Parallel_Perpendicular(bounds,
							feature_Perpendicular_Parallel_Direction);
					break;
				}
			}
			if (!bFindFeature) {
				this.undo();
			}
		}
		break;
	default:
		break;
	}

	if (_begin_Draw) {
		this.undo();
		var objComponentLength = this.handler.getGeometry().components[0];
		if (objComponentLength.components.length > 1) {
			var objComponents = objComponentLength.components;
			var indexComponents = objComponents.length - 2;
			var bSame_Coordinate = true;
			while (bSame_Coordinate) {
				if ((objComponents[indexComponents + 1].x != objComponents[indexComponents].x)
						|| (objComponents[indexComponents + 1].y != objComponents[indexComponents].y)) {
					bSame_Coordinate = false;
				} else {
					indexComponents -= 1;
				}
			}
			for (i = indexComponents + 1; i < objComponents.length; i++) {
				objComponents[i].x = objComponents[indexComponents].x;
				objComponents[i].y = objComponents[indexComponents].y;
			}
			if (pointLast_Path != null) {
				pointLast_Path.x = objComponents[objComponents.length - 1].x;
				pointLast_Path.y = objComponents[objComponents.length - 1].y;
			}
			// var point1 = objComponents[indexComponents];
			// var point2 = objComponents[indexComponents - 1];
			// var vector_Line = create_Vector(point1, point2);
			// dGoc_FeatureSelected_Parallel_Perpendicular =
			// Goc_DuongThang(vector_Line);
			// if (dGoc_FeatureSelected_Parallel_Perpendicular > 180)
			// dGoc_FeatureSelected_Parallel_Perpendicular -= 180;
			// if ((point1.x > point2.x) && (point1.y > point2.y)) {
			// dGoc_FeatureSelected_Parallel_Perpendicular += 180;
			// }
			// else if ((point1.x > point2.x) && (point1.y < point2.y)) {
			// }
			// if ((point1.x < point2.x) && (point1.y > point2.y)) {
			// dGoc_FeatureSelected_Parallel_Perpendicular -= 180;
			// }
			// if ((point1.x < point2.x) && (point1.y < point2.y)) {
			// }
		}

		bUndo_First = true;
		_begin_Draw = false;
	} else {
		bUndo_First = true;
		gPointCount++;
		pointArr.push(aPoint);
		pointLast_Path = aPoint;
		_begin_Draw = false;
	}
}

// event mouse move on map when draw
function mousemoveHandler_Path(evt) {
	if (!_begin_Draw) {
		if (this.line != null) {
			var bounds = map.getLonLatFromPixel(evt.xy);
			if (!bUndo_First) {
				this.undo();
			}
			bUndo_First = false;
			this.insertXY(bounds.lon, bounds.lat);
		}
	} else {
		bUndo_First = false;
		this.undo();
		var dLength = 0;
		var point = pointLast_Path; // pointArr[pointArr.length - 1];
		if (point == null) {
			_begin_Draw = false;
			return true;
		}
		var bounds = map.getLonLatFromPixel(evt.xy);
		dLength = Math.pow(point.x - bounds.lon, 2)
				+ Math.pow(point.y - bounds.lat, 2);
		dLength = Math.sqrt(dLength);

		var dGoc = 0;
		if (Select_Extension == Portlet_Type_Action.Add_Line_Extension_Angle) {
			dGoc = dGoc_Path;
		} else if (Select_Extension == Portlet_Type_Action.Add_Line_Extension_Parallel) {
			dGoc = dGoc_FeatureSelected_Parallel_Perpendicular;
		}
		if (dGoc < 0)
			dGoc = Number(dGoc) + 360;
		// A: 0 < A < 90
		// B: 90 < B < 180
		// C: 180 < C < 270
		// D: 270 < D < 360
		if (dGoc <= 90) {
			// khu A
			if (bounds.lat > point.y && bounds.lon > point.x) {
			}
			// khu B
			else if (bounds.lat > point.y && bounds.lon < point.x) {
				dGoc = parseInt(dGoc) + 180;
			}
			// khu C
			else if (bounds.lat < point.y && bounds.lon < point.x) {
				dGoc = parseInt(dGoc) + 180;
			}
			// khu D
			else {
			}
		} else if (dGoc <= 180) {
			// khu A
			if (bounds.lat > point.y && bounds.lon > point.x) {
				dGoc = parseInt(dGoc) + 180;
			}
			// khu B
			else if (bounds.lat > point.y && bounds.lon < point.x) {
			}
			// khu C
			else if (bounds.lat < point.y && bounds.lon < point.x) {
			}
			// khu D
			else {
				dGoc = parseInt(dGoc) + 180;
			}
		} else if (dGoc <= 270) {
			// khu A
			if (bounds.lat > point.y && bounds.lon > point.x) {
				dGoc = parseInt(dGoc) - 180;
			}
			// khu B
			else if (bounds.lat > point.y && bounds.lon < point.x) {
			}
			// khu C
			else if (bounds.lat < point.y && bounds.lon < point.x) {
			}
			// khu D
			else {
				dGoc = parseInt(dGoc) - 180;
			}
		} else {
			// khu A
			if (bounds.lat > point.y && bounds.lon > point.x) {
			}
			// khu B
			else if (bounds.lat > point.y && bounds.lon < point.x) {
				dGoc = parseInt(dGoc) - 180;
			}
			// khu C
			else if (bounds.lat < point.y && bounds.lon < point.x) {
				dGoc = parseInt(dGoc) - 180;
			}
			// khu D
			else {
			}
		}
		this.insertDirectionLength(dGoc, dLength);
	}
}

/** **** Angle ***** */
var dGoc_Path = 0, dLength_Path = 0;
function change_Goc(e) {
	var _txtGoc = _getElementById('txt_Map_Goc');
	if (!e.checked) {
		_txtGoc.disabled = false;
	} else {
		_txtGoc.disabled = true;
	}
	return false;
}

function change_DoDai(e) {
	var _txtChieuDai = _getElementById('txt_Map_ChieuDai');
	if (!e.checked) {
		_txtChieuDai.disabled = false;
	} else {
		_txtChieuDai.disabled = true;
	}
	return false;
}

function btn_Map_OK_Direction_Click() {
	var _txtGoc = _getElementById('txt_Map_Goc');
    var _txtChieuDai = _getElementById('txt_Map_ChieuDai');
    var _chkGoc = _getElementById('chk_Map_Goc');
    var _chkChieuDai = _getElementById('chk_Map_Length');
    var _chkRelative = _getElementById('chk_Map_Relative');
    var bGoc = false, bLength = false, bRelative = false;
    bGoc = _chkGoc.checked;
    bLength = _chkChieuDai.checked;
    bRelative = _chkRelative.checked;
    bDisable = _chkRelative.disabled;
    if (!bGoc && !bLength) {
        alert('Bạn cần chọn góc hoặc chiều dài để vẽ.');
        return false;
    }
    dGoc_Path = _txtGoc.value;
    if (dGoc_Path == '') {
        alert('Bạn cần nhập góc.');
        return false;
    }
    dLength_Path = _txtChieuDai.value;
    if (dLength_Path == '') {
        alert('Bạn cần nhập chiều dài.');
        return false;
    }

    if ((bRelative) && (!bDisable)) dGoc_Path = Number(dGoc_Path) + dGoc_FeatureSelected_Parallel_Perpendicular;
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	indexLayer = indexLayer - 1 - map_WMS.length;
    if (bGoc && bLength) {
        var values = dGoc_Path + ',' + dLength_Path;
        values = values.split(',');
        if (values != null) {
            Draw_Obj[indexLayer].undo();
            Draw_Obj[indexLayer].insertDirectionLength(values[0], values[1]);
        }
    }
    else if (bGoc) {
        //Draw_Obj[indexLayer - 1].undo();
        _begin_Draw = true;
    }
    else if (bLength) {
        Draw_Obj[indexLayer].undo();
        Draw_Obj[indexLayer].insertDirectionLength(0, dLength_Path);
    }
    var objComponentLength = Draw_Obj[indexLayer].handler.getGeometry().components[0].components;
    var indexComponents = objComponentLength.length - 2;
    var bSame_Coordinate = true;
    while (bSame_Coordinate) {
        if ((objComponentLength[indexComponents + 1].x != objComponentLength[indexComponents].x) || (objComponentLength[indexComponents + 1].y != objComponentLength[indexComponents].y)) {
            bSame_Coordinate = false;
        }
        else indexComponents -= 1;
    }
    var i=0;
    // cap nhat diem cuoi
    if (bLength) {
        for (i = indexComponents + 1; i < objComponentLength.length; i++) {
            objComponentLength[i].x = objComponentLength[indexComponents].x;
            objComponentLength[i].y = objComponentLength[indexComponents].y;
        }
    }
    var point1 = objComponentLength[indexComponents];
    var point2 = objComponentLength[indexComponents + 1];
    var vector_Line = create_Vector(point1, point2);
    dGoc_FeatureSelected_Parallel_Perpendicular = Goc_DuongThang(vector_Line);
    if (dGoc_FeatureSelected_Parallel_Perpendicular > 180) dGoc_FeatureSelected_Parallel_Perpendicular -= 180;
    // tu phai qua trai & di xuong duoi - khu A
    if ((point1.x > point2.x) && (point1.y > point2.y)) {
        dGoc_FeatureSelected_Parallel_Perpendicular += 180;
    }
    // tu phai qua trai & di len tren - khu D
    else if ((point1.x > point2.x) && (point1.y < point2.y)) {
    }
    // tu trai qu phrai & di xuong duoi - khu B
    if ((point1.x < point2.x) && (point1.y > point2.y)) {
        dGoc_FeatureSelected_Parallel_Perpendicular -= 180;
    }
    // tu trai qu phrai & di len tren - khu C
    if ((point1.x < point2.x) && (point1.y < point2.y)) {
    }
    bUndo_First = true;
    _divMenuAction_Current.divChild = _div_Map_divAddFeature_Attribute;
    return false;
}
/** **************** */

/** **** Parallel ***** */
function change_DoDai_Parallel(e) {
	var _txtChieuDai_Parallel = _getElementById('txt_Map_ChieuDai_Parallel');
	if (!e.checked) {
		_txtChieuDai_Parallel.disabled = false;
	} else {
		_txtChieuDai_Parallel.disabled = true;
	}
	return false;
}

function btnMapOK_Parallel() {
	var i = 0;
	var _chkLength_Parallel = _getElementById('chk_Map_Length_Parallel');
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	indexLayer = indexLayer - 1 - map_WMS.length;
	if (_chkLength_Parallel.checked) {
		_txtChieuDai_Parallel = _getElementById('txt_Map_ChieuDai_Parallel');
		objValue = parseInt(_txtChieuDai_Parallel.value);
		Draw_Obj[indexLayer].undo();
		Draw_Obj[indexLayer].insertDirectionLength(
				dGoc_FeatureSelected_Parallel_Perpendicular, objValue);
	} else {
		_begin_Draw = true;
	}
	var objComponentLength = Draw_Obj[indexLayer].handler.getGeometry().components[0].components;
	var indexComponents = objComponentLength.length - 2;
	var bSame_Coordinate = true;
	while (bSame_Coordinate) {
		if ((objComponentLength[indexComponents + 1].x != objComponentLength[indexComponents].x)
				|| (objComponentLength[indexComponents + 1].y != objComponentLength[indexComponents].y)) {
			bSame_Coordinate = false;
		} else
			indexComponents -= 1;
	}
	// cap nhat diem cuoi
	if (_chkLength_Parallel.checked) {
		for (i = indexComponents + 1; i < objComponentLength.length; i++) {
			objComponentLength[i].x = objComponentLength[indexComponents].x;
			objComponentLength[i].y = objComponentLength[indexComponents].y;
		}
		if (pointLast_Path != null) {
			pointLast_Path.x = objComponentLength[objComponentLength.length - 1].x;
			pointLast_Path.y = objComponentLength[objComponentLength.length - 1].y;
		}
	}
	bUndo_First = true;
    _divMenuAction_Current.divChild = _div_Map_divAddFeature_Attribute;
}
/** ******* End Parallel ************ */

/** ******** Distance ************ */
/** ******** End Distance *********** */

/** ******** Vertex ***************
 finish
/** ******** End Vertex ********* */

/** ******** Copy ************* */
/** ******** End Copy ********** */
/** **************************** */

/** ***** Edit vertex line & area ******* **
finish
/** ************************************* */

/** ****** Split feature ******* */
var bConfirm_Split = false;
function flashFeatures(features, index) {
	if (!index) {
		index = 0;
		/*
		 * answer = confirm("Bạn có muốn chia đoạn này không?"); if (answer)
		 * bConfirm_Split = true; else bConfirm_Split = false;
		 */
	}
	// if (!bConfirm_Split) return;
	var current = features[index];
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	var layer_Split = map.layers[indexLayer];
	if (current && current.layer === layer_Split) {
		layer_Split.drawFeature(features[index], "select");
	}
	var prev = features[index - 1];
	if (prev && prev.layer === layer_Split) {
		layer_Split.drawFeature(prev, "default");
	}
	++index;
	if (index <= features.length) {
		window.setTimeout(function() {
			flashFeatures(features, index);
		}, 500);
	}
}
/** **************************** */

/** ****** Merge feature ******* */
var objFeature_Merge_Extent_Trim_Current = null;
var arrComponent_Merge = new Array();
var arrFeature_Merge_Extent_Trim = new Array();
var bAction_Merge_Extent_Trim = false;
function MergeFeature_Close()
{
	var divName = '';
	var tbodyName = '';
	if (Action_Current == Portlet_Type_Action.Merge) {
	    tbodyName = 'tbody_Map_Merge_Feature';
	    divName = 'div_Map_Merger_Feature';
	}
	else if (Action_Current == Portlet_Type_Action.Extent) {
	    tbodyName = 'tbody_Map_Extent_Feature';
	    divName = 'div_Map_Extent_Feature';
	}
	else if (Action_Current == Portlet_Type_Action.Trim) {
	    tbodyName = 'tbody_Map_Trim_Feature';
	    divName = 'div_Map_Trim_Feature';
	}
	
	var tbody_Feature = _getElementById(tbodyName);
	if (tbody_Feature.hasChildNodes()) {
	    while (tbody_Feature.childNodes.length >= 1) {
	        tbody_Feature.removeChild(tbody_Feature.firstChild);
	    }
	}
	var divFeature = _getElementById(divName);
	divFeature.style.display = 'none';
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	var layer_Feature = map.layers[indexLayer];
	for (var i = 0; i < arrFeature_Merge_Extent_Trim.length; i++) {
	    layer_Feature.drawFeature(arrFeature_Merge_Extent_Trim[i], "default");
	}
	if (objFeature_Merge_Extent_Trim_Current != null) layer_Feature.drawFeature(objFeature_Merge_Extent_Trim_Current, "default");
	if (Action_Current == Portlet_Type_Action.Merge) {
	    arrComponent_Merge = new Array();
	}
	else if (Action_Current == Portlet_Type_Action.Extent) {
	}
	else if (Action_Current == Portlet_Type_Action.Trim) {
	}
	indexFeature_Extent_Trim = 0;
	arrFeature_Merge_Extent_Trim = new Array();
}

function btnMerge_Extent_Trim_Action_Click() {
    if (objFeature_Merge_Extent_Trim_Current == null) {
        alert('Bạn cần chọn 1 đối tượng.');
        return;
    }

    var objComponent = null;
    var i = 0, j = 0, k = 0;
    var tbodyName = '';
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
    var layer_Feature = map.layers[indexLayer];
    bAction_Merge_Extent_Trim = true;
    // Merge
    if (Action_Current == Portlet_Type_Action.Merge) {
        tbodyName = 'tbody_Map_Merge_Feature';        
        // xoa phan tu con lai de merge
        for (i = 0; i < arrFeature_Merge_Extent_Trim.length; i++) {
            objComponent = arrFeature_Merge_Extent_Trim[i];
            if (objComponent.fid != objFeature_Merge_Extent_Trim_Current.fid) {
                arrComponent_Merge[arrComponent_Merge.length] = objComponent;
            }
        }
        
        // merge
        var geometry_Merge = objFeature_Merge_Extent_Trim_Current.geometry;
        for (i = 0; i < arrComponent_Merge.length; i++) {
            var pPoint = null;
            var objComponent1 = new Array();
            for (j = 0; j < arrComponent_Merge[i].geometry.components.length; j++) {
                objComponent = arrComponent_Merge[i].geometry.components[j].components;
                geometry_Merge.componentTypes[geometry_Merge.componentTypes.length] = "OpenLayers.Geometry.LineString";
                for (k = 0; k < objComponent.length; k++) {
                    pPoint = new OpenLayers.Geometry.Point();
                    pPoint.x = objComponent[k].x;
                    pPoint.y = objComponent[k].y;
                    objComponent1[objComponent1.length] = pPoint;
                }
                geometry_Merge.components[geometry_Merge.components.length] = new OpenLayers.Geometry.LineString();
                geometry_Merge.components[geometry_Merge.components.length - 1].components = objComponent1;
                geometry_Merge.components[geometry_Merge.components.length - 1].bounds = arrComponent_Merge[i].geometry.components[j].bounds;
            }
            featureRemove(arrComponent_Merge[i]);
            arrComponent_Merge[i].state = OpenLayers.State.DELETE;
        }
        layer_Feature.drawFeature(objFeature_Merge_Extent_Trim_Current, 'select');
        objFeature_Merge_Extent_Trim_Current.state = OpenLayers.State.UPDATE;
    }
    // Extent
    else if (Action_Current == Portlet_Type_Action.Extent) {
        tbodyName = 'tbody_Map_Extent_Feature';
    }
    // Trim
    else if (Action_Current == Portlet_Type_Action.Trim) {
        tbodyName = 'tbody_Map_Trim_Feature';
        var feature_Trim = objFeature_Merge_Extent_Trim_Current;
        var feature_Other = null;
        for (i = 0; i < arrFeature_Merge_Extent_Trim.length; i++) {
            if (feature_Trim.fid != arrFeature_Merge_Extent_Trim[i].fid) {
                feature_Other = arrFeature_Merge_Extent_Trim[i];
                break;
            }
        }
        if (!feature_Trim.geometry.intersects(feature_Other.geometry)) {
            alert('2 đối tượng bạn chọn không giao nhau.');
            indexFeature_Extent_Trim = 0;
            var tbody_Feature = _getElementById(tbodyName);
            if (tbody_Feature.hasChildNodes()) {
                while (tbody_Feature.childNodes.length >= 1) {
                    tbody_Feature.removeChild(tbody_Feature.firstChild);
                }
            }
            objFeature_Merge_Extent_Trim_Current = null;
            arrFeature_Merge_Extent_Trim = new Array();
            ctrlSelectFeatures.activate();
            indexFeature_Extent_Trim = 0;
            return;
        }
    }

    if (Action_Current == Portlet_Type_Action.Merge) {
        // xoa phan tu trong tbody
        var tbody_Feature = _getElementById(tbodyName);
        if (tbody_Feature.hasChildNodes()) {
            while (tbody_Feature.childNodes.length >= 1) {
                tbody_Feature.removeChild(tbody_Feature.firstChild);
            }
        }
        var tr = document.createElement('tr');
        var td = document.createElement('td');
        var objFeature = objFeature_Merge_Extent_Trim_Current;
        if (!td.textContent) td.innerText = objFeature.fid;
        else td.textContent = objFeature.fid;
        td.feature_merge = objFeature;
        td.onclick = td_Merge_feature_Click;
        td.onmousemove = td_Merge_feature_MouseMove;
        td.onmouseout = td_Merge_feature_MouseOut;
        tr.appendChild(td);
        tbody_Feature.appendChild(tr);
        arrComponent_Merge = new Array();
        ctrlSelectFeatures.activate();
        arrFeature_Merge_Extent_Trim = new Array();
    }
    else if (Action_Current == Portlet_Type_Action.Extent) {
    }
    else if (Action_Current == Portlet_Type_Action.Trim) {
    }
}

function btnMerge_Extent_Trim_Cancel_Click() {
    var tbodyName = '';
    if (Action_Current == Portlet_Type_Action.Merge) {
        tbodyName = 'tbody_Map_Merge_Feature';
    }
    else if (Action_Current == Portlet_Type_Action.Extent) {
        tbodyName = 'tbody_Map_Extent_Feature';
    }
    else if (Action_Current == Portlet_Type_Action.Trim) {
        tbodyName = 'tbody_Map_Trim_Feature';
    }

    // xoa phan tu trong tbody
    var tbody_Feature = _getElementById(tbodyName);
    if (tbody_Feature.hasChildNodes()) {
        while (tbody_Feature.childNodes.length >= 1) {
            tbody_Feature.removeChild(tbody_Feature.firstChild);
        }
    }
    ctrlSelectFeatures.activate();
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
    var layer_Feature = map.layers[indexLayer];
    for (var i = 0; i < arrFeature_Merge_Extent_Trim.length; i++) {
        layer_Feature.drawFeature(arrFeature_Merge_Extent_Trim[i], "default");
    }
    if (objFeature_Merge_Extent_Trim_Current != null) layer_Feature.drawFeature(objFeature_Merge_Extent_Trim_Current, "default");
    if (Action_Current == Portlet_Type_Action.Merge) {
        arrComponent_Merge = new Array();
    }
    else if (Action_Current == Portlet_Type_Action.Extent) {
    }
    else if (Action_Current == Portlet_Type_Action.Trim) {
    }
    indexFeature_Extent_Trim = 0;
    arrFeature_Merge_Extent_Trim = new Array();
}

function td_Merge_feature_Click() {
    var objTD = this;
    var objFeature = objTD.feature_merge;
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
    var layer_Feature = map.layers[indexLayer];
    if (objFeature_Merge_Extent_Trim_Current != null) layer_Feature.drawFeature(objFeature_Merge_Extent_Trim_Current, "select");
    layer_Feature.drawFeature(objFeature, "delete_selection");
    objFeature_Merge_Extent_Trim_Current = objFeature;
    bAction_Merge_Extent_Trim = true;
}

function td_Merge_feature_MouseMove() {
    var objTD = this;
    objTD.style.backgroundColor = '#E0FFFF';
}

function td_Merge_feature_MouseOut() {
    var objTD = this;
    objTD.style.backgroundColor = '';
}

var indexFeature_Extent_Trim = 0;
function selectedFeature_Merge(param) {
    indexFeature_Extent_Trim++;
    var divName = '';
    if (Action_Current == Portlet_Type_Action.Merge) {
        divName = 'tbody_Map_Merge_Feature';
    }
    else if (Action_Current == Portlet_Type_Action.Extent) {
        divName = 'tbody_Map_Extent_Feature';
    }
    else if (Action_Current == Portlet_Type_Action.Trim) {
        divName = 'tbody_Map_Trim_Feature';
        if (indexFeature_Extent_Trim > 2) {
            alert('Bạn chỉ được chọn 2 đối tượng');
        	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
            var layer_Feature = map.layers[indexLayer];
            layer_Feature.drawFeature(param, "default");
            return;
        }
    }
    // xoa phan tu trong tbody
    var tbody_Feature = _getElementById(divName);
    if (bAction_Merge_Extent_Trim) {
        bAction_Merge_Extent_Trim = false;
        if (tbody_Feature.hasChildNodes()) {
            while (tbody_Feature.childNodes.length >= 1) {
                tbody_Feature.removeChild(tbody_Feature.firstChild);
            }
        }
    }
    arrFeature_Merge_Extent_Trim[arrFeature_Merge_Extent_Trim.length] = param;
    var tr = document.createElement('tr');
    var td = document.createElement('td');
    var objFeature = param;
    if (!td.textContent) td.innerText = objFeature.fid;
    else td.textContent = objFeature.fid;
    td.feature_merge = objFeature;
    td.onclick = td_Merge_feature_Click;
    td.onmousemove = td_Merge_feature_MouseMove;
    td.onmouseout = td_Merge_feature_MouseOut;
    tr.appendChild(td);
    tbody_Feature.appendChild(tr);
    ctrlSelectFeatures.deactivate();
}
/** **************************** */

/** ****** Copy feature ******* */
/** *************************** */
/************ end function extension ***************/
